<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="广告类型" prop="positiontype">
        <el-select v-model="dataForm.positiontype" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in positiontypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      <!-- <el-input v-model="dataForm.positiontype" placeholder="广告类型"></el-input> -->
      </el-form-item>
      <el-form-item label="广告位置名称" prop="positionname">
        <el-input v-model="dataForm.positionname" placeholder="广告位置名称" />
      </el-form-item>
      <el-form-item label="建议宽度" prop="positionwidth">
        <el-input v-model="dataForm.positionwidth" placeholder="建议宽度" />
      </el-form-item>
      <el-form-item label="建议高度" prop="positionheight">
        <el-input v-model="dataForm.positionheight" placeholder="建议高度" />
      </el-form-item>
      <el-form-item label="广告位置代码" prop="positioncode">
        <el-input v-model="dataForm.positioncode" placeholder="广告位置代码" />
      </el-form-item>
      <el-form-item label="排序号" prop="apsort">
        <el-input v-model="dataForm.apsort" placeholder="排序号" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
      <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isNumber } from '@/utils/validate'
export default {
  data() {
    var isNum = (rule, value, callback) => {
      if (!isNumber(value)) {
        callback(new Error('请输入数字值'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      positiontypes: [{
        value: 1,
        label: 'PC'
      }, {
        value: 2,
        label: 'Mobile'
      }],
      dataForm: {
        positionid: 0,
        positiontype: '',
        positionname: '',
        positionwidth: '',
        positionheight: '',
        dataflag: '',
        positioncode: '',
        apsort: '',
        createtime: '',
        creator: '',
        updatetime: '',
        updater: ''
      },
      dataRule: {
        positiontype: [
          { required: true, message: '广告类型不能为空', trigger: 'blur' }
        ],
        positionname: [
          { required: true, message: '广告位置名称不能为空', trigger: 'blur' }
        ],
        positionwidth: [ // isNumber
          { required: true, message: '建议宽度不能为空', trigger: 'blur' },
          { validator: isNum, trigger: 'blur' }
        ],
        positionheight: [
          { required: true, message: '建议高度不能为空', trigger: 'blur' },
          { validator: isNum, trigger: 'blur' }
        ],
        dataflag: [
          { required: true, message: '是否删除不能为空', trigger: 'blur' }
        ],
        positioncode: [
          { required: true, message: '广告位置代码不能为空', trigger: 'blur' }
        ],
        apsort: [
          { required: true, message: '排序号不能为空', trigger: 'blur' },
          { validator: isNum, trigger: 'blur' }
        ],
        createtime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        creator: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        updatetime: [
          { required: true, message: '修改时间不能为空', trigger: 'blur' }
        ],
        updater: [
          { required: true, message: '修改人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.positionid = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.positionid) {
          this.$http({
            url: this.$http.adornUrl(`/cms/adpositions/info/${this.dataForm.positionid}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.positiontype = data.AdPosition.positiontype
              this.dataForm.positionname = data.AdPosition.positionname
              this.dataForm.positionwidth = data.AdPosition.positionwidth
              this.dataForm.positionheight = data.AdPosition.positionheight
              this.dataForm.dataflag = data.AdPosition.dataflag
              this.dataForm.positioncode = data.AdPosition.positioncode
              this.dataForm.apsort = data.AdPosition.apsort
              this.dataForm.createtime = data.AdPosition.createtime
              this.dataForm.creator = data.AdPosition.creator
              this.dataForm.updatetime = data.AdPosition.updatetime
              this.dataForm.updater = data.AdPosition.updater
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/cms/adpositions/${!this.dataForm.positionid ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'positionid': this.dataForm.positionid || undefined,
              'positiontype': this.dataForm.positiontype,
              'positionname': this.dataForm.positionname,
              'positionwidth': this.dataForm.positionwidth,
              'positionheight': this.dataForm.positionheight,
              'dataflag': this.dataForm.dataflag,
              'positioncode': this.dataForm.positioncode,
              'apsort': this.dataForm.apsort,
              'createtime': this.dataForm.createtime,
              'creator': this.dataForm.creator,
              'updatetime': this.dataForm.updatetime,
              'updater': this.dataForm.updater
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
